package com.yangyang.ralearn.message;

import java.util.List;

import org.jboss.netty.channel.Channel;

import com.yangyang.ralearn.common.JEnvironment;
import com.yangyang.ralearn.common.entity.base.AbstractDomainPredicate;
import com.yangyang.ralearn.net.message.AbstractReqMessage;
import com.yangyang.ralearn.net.message.Message;
import com.yangyang.ralearn.net.message.MessageFactory;
import com.yangyang.ralearn.net.message.MessageId;
import com.yangyang.ralearn.net.serialize.AutoSerializable;
import com.yangyang.ralearn.server.JSimulator;
import com.yangyang.ralearn.server.ServerManager;
import com.yangyang.ralearn.util.Object2String;

@Message(id = MessageId.REQ_CREATE_GROUP)
public class ReqCreateGroupMessage extends AbstractReqMessage {

	@AutoSerializable(index = 1)
	private String domainFile;
	@AutoSerializable(index = 2)
	private String problemFile;

	public ReqCreateGroupMessage() {
		domainFile = "";
		problemFile = "";
	}

	public void init(String domainFile, String problemFile) {
		this.domainFile = domainFile;
		this.problemFile = problemFile;
	}

	@Override
	public void onGetClientMessage(int groupId, int simulatorId, Channel channel) {
		// TODO Auto-generated method stub
		groupId = ServerManager.instance().newGroup(groupId, domainFile,
				simulatorId, problemFile, channel);
		if (groupId > 0) {
			RespCreateGroupMessage message = (RespCreateGroupMessage) MessageFactory
					.instance().createMessage(
							MessageId.encodeId(MessageId.RESP_CREATE_GROUP, groupId, simulatorId));
			message.init(
					new Object2String<JEnvironment>()
							.object2String(ServerManager.instance()
									.getEnvironmentById(groupId)),
					new Object2String<List<AbstractDomainPredicate>>()
							.object2String(ServerManager.instance()
									.getSimulatorById(groupId, simulatorId).goalStates));
			ServerManager.instance().sendMessage(groupId, message,
					(Integer[]) null);
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
